﻿Imports WINX32Lib 'Remember to add r3eference first (Add Reference option). For MS Visual Studio .NET 2003: before adding a reference to the project, corresponding assembly should be added to the Add Reference dialog box (http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.71%29.aspx).
Imports System.IO
Imports System.Math

Module Functions


    'Save_Each must divie both: Total_Number_Of_Frames and Max_No_Of_Frames_Per_File
    Public Sub Acquisition(ByVal Total_Number_Of_Frames As Integer, ByVal Max_No_Of_Frames_Per_File As Integer, ByVal Save_Each As Integer, _
                         ByVal File_Path As String, ByVal File_Name As String, ByVal Acq_Setup As ExpSetup)

        Dim Number_Of_Files As Integer = Ceiling(Total_Number_Of_Frames / Max_No_Of_Frames_Per_File)
        Dim Acqusition_Running As Boolean = True

        For i As Integer = 0 To Number_Of_Files - 1 'The first loop does hard saves of files (each with Max_No_Of_Frames_Per_File frames)
            Dim Final_Number_Of_Frames_In_File As Integer = Min(Max_No_Of_Frames_Per_File, Total_Number_Of_Frames - i * Max_No_Of_Frames_Per_File)
            Dim Number_Of_File_Saves As Integer = Ceiling(Final_Number_Of_Frames_In_File / Save_Each)
            Dim Acq_File As New DocFile

            If i = 0 Then
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & ".spe")
            Else
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & i & ".spe")
            End If

            For j As Integer = 0 To Number_Of_File_Saves - 1 'The second loop fills each file gradually (by Save_Each frames)

                If j = 0 Then
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 1) 'Overwrite
                Else
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 2) 'Append to
                End If

                'Me.Hide()
                Acq_Setup.Start(Acq_File)

                Dim Number_Of_Frames_In_File As Integer = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) 'the number of frames in Acq_File after appending frames by Acq_Setup.Start(Acq_File) 

                While Acq_Setup.GetParam(EXP_CMD.EXP_RUNNING_EXPERIMENT) = 1
                End While

                'Terminating the acquisition by user changes the DM_CMD.DM_NUMFRAMES parameter
                If Not Number_Of_Frames_In_File = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) Then
                    Acqusition_Running = False
                    Exit For
                End If

                'Me.Show()
            Next
            If Not Acqusition_Running Then
                'Me.Show()
                Exit For
            End If
            Acq_File.Close() 'Not closing files while creating others causes virtual memory to run out of space - soon MapViewOfFile interruption is likely to occur (e.g. after 1100 frames acquired in total)
        Next

    End Sub

    'Acquisition that take arbitrary valueas of Total_Number_Of_Frames Max_No_Of_Frames_Per_File  Save_Each
    Public Sub Acquisition1(ByVal Total_Number_Of_Frames As Integer, ByVal Max_No_Of_Frames_Per_File As Integer, ByVal Save_Each As Integer, _
                        ByVal File_Path As String, ByVal File_Name As String, ByVal Acq_Setup As ExpSetup)

        Dim Number_Of_Files As Integer = Ceiling(Total_Number_Of_Frames / Max_No_Of_Frames_Per_File)
        Dim Acqusition_Running As Boolean = True

        For i As Integer = 0 To Number_Of_Files - 1 'The first loop does hard saves of files (each with Max_No_Of_Frames_Per_File frames)
            Dim Final_Number_Of_Frames_In_File As Integer = Min(Max_No_Of_Frames_Per_File, Total_Number_Of_Frames - i * Max_No_Of_Frames_Per_File)
            Dim Number_Of_File_Saves As Integer = Ceiling(Final_Number_Of_Frames_In_File / Save_Each)

            Dim Acq_Setup2 As New ExpSetup 'to change the EXP_CMD.EXP_SEQUENTS parameter the creation of new ExpSetup is neccessary
            Acq_Setup2.SetParam(EXP_CMD.EXP_SEQUENTS, Min(Save_Each, Final_Number_Of_Frames_In_File))


            Dim Acq_File As New DocFile ' we want the dafaoult parameters to be load fo this the Acq_Setup needs to be defined

            If i = 0 Then
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & ".spe")
            Else
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & i & ".spe")
            End If



            For j As Integer = 0 To Number_Of_File_Saves - 1 'The second loop fills each file gradually (by Save_Each frames)

                Dim Number_Of_Sequences As Integer

                If (j + 1) * Save_Each < Final_Number_Of_Frames_In_File Then
                    Number_Of_Sequences = Save_Each
                Else
                    Number_Of_Sequences = Final_Number_Of_Frames_In_File - (Number_Of_File_Saves - 1) * Save_Each
                End If


                If j = 0 Then
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 1) 'Overwrite
                Else
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 2) 'Append to
                End If

                Dim Acq_Setup3 As New ExpSetup 'to change the EXP_CMD.EXP_SEQUENTS parameter the creation of new ExpSetup is neccessary
                Acq_Setup3.SetParam(EXP_CMD.EXP_SEQUENTS, Number_Of_Sequences)

                'Me.Hide()
                Acq_Setup.Start(Acq_File)

                Dim Number_Of_Frames_In_File As Integer = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) 'the number of frames in Acq_File after appending frames by Acq_Setup.Start(Acq_File) 

                While Acq_Setup.GetParam(EXP_CMD.EXP_RUNNING_EXPERIMENT) = 1
                End While

                'Terminating the acquisition by user changes the DM_CMD.DM_NUMFRAMES parameter
                If Not Number_Of_Frames_In_File = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) Then
                    Acqusition_Running = False
                    Exit For
                End If

                'Me.Show()
            Next
            If Not Acqusition_Running Then
                'Me.Show()
                Exit For
            End If
            Acq_File.Close() 'Not closing files while creating others causes virtual memory to run out of space - soon MapViewOfFile interruption is likely to occur (e.g. after 1100 frames acquired in total)
        Next

    End Sub

    Sub Analize(ByVal Acq_File_Path As String, ByVal No_Of_Frame_To_Analize As Integer, ByVal low As Short, ByVal high As Short, ByVal low1 As Short, ByVal high1 As Short, _
                Optional ByVal Histograme_File_Path As String, Optional ByVal Hits_File_Path As String)
        'the Acq_File is given by its file path to facilitate the use of teh function as a command line programe. 
        Dim Acq_File As New DocFile
        Acq_File.Open(Acq_File_Path)
        Dim Frame_To_Analize As Object
        Acq_File.GetFrame(No_Of_Frame_To_Analize, Frame_To_Analize)

        If Not Frame_To_Analize.Rank = 1 Then
            MsgBox("The frame to analize hase a dimention of " & Frame_To_Analize.Rank)
        End If


        Dim Neighbours(,) As Boolean
        ReDim Neighbours(Frame_To_Analize.GetLength(0), Frame_To_Analize.GetLength(2))
        Dim Max_Intensity = 0
        Dim Max_Multiplicity As Short = 0
        Dim Intensity_Histograme(,) As Short


        For i As Integer = 0 To Frame_To_Analize.GetLength(0)
            For j As Integer = 0 To Frame_To_Analize.GetLength(1)
                Max_Intensity = Max(Max_Intensity, Frame_To_Analize(i, j))
            Next
        Next

        ReDim Intensity_Histograme(Max_Intensity, 1)

        Dim Multiple_Hits(,,) As Short
        ReDim Multiple_Hits(Frame_To_Analize.GetLength(0), Frame_To_Analize.GetLength(2), Floor((high - low) / low) + 1)
        Dim Total_Number_of_good_hits() As Integer
        ReDim Total_Number_of_good_hits(Floor((high - low) / low) + 1)

        For i As Integer = 0 To Frame_To_Analize.GetLength(0)
            For j As Integer = 0 To Frame_To_Analize.GetLength(1)
                Intensity_Histograme(Frame_To_Analize(i, j), 0) = Intensity_Histograme(Frame_To_Analize(i, j), 0) + 1
                Intensity_Histograme(Frame_To_Analize(i, j), 1) = Intensity_Histograme(Frame_To_Analize(i, j), 1) + 1

                'here we check the multiplicity of the hit
                Dim Lower_Lewel_Multiplicity As Integer = Floor(Frame_To_Analize(i, j) / low)
                Dim Upper_Lewel_Multiplicity As Integer = Ceiling(Frame_To_Analize(i, j) / high)

                If Lower_Lewel_Multiplicity >= Upper_Lewel_Multiplicity Then 'one should be creful to satisfy the condition : high < 2*low
                    If Lower_Lewel_Multiplicity < Floor((high - low) / low) Then
                        Multiple_Hits(i, j, 0) = Lower_Lewel_Multiplicity
                        Multiple_Hits(i, j, Lower_Lewel_Multiplicity) = 1
                        Total_Number_of_good_hits(Lower_Lewel_Multiplicity) = Total_Number_of_good_hits(Lower_Lewel_Multiplicity) + 1
                        Total_Number_of_good_hits(0) = Total_Number_of_good_hits(0) + Lower_Lewel_Multiplicity
                        Frame_To_Analize(i, j) = 0
                        Max_Multiplicity = Max(Max_Multiplicity, Lower_Lewel_Multiplicity)
                    End If
                End If


                If i > 0 And j > 0 Then
                    Dim Neighbours_Sum As Integer = Frame_To_Analize(i - 1, j - 1) + Frame_To_Analize(i - 1, j) + Frame_To_Analize(i, j - 1) + Frame_To_Analize(i, j)
                    Neighbours(i, j) = (Neighbours_Sum < high1) And (Neighbours_Sum > low1)
                    If Neighbours(i, j) Then
                        Multiple_Hits(i, j, 1) = Abs(CShort(Neighbours(i, j)))
                        Multiple_Hits(i, j, 0) = Multiple_Hits(i, j, 0) + Multiple_Hits(i, j, 1)
                        Intensity_Histograme(Neighbours_Sum, 1) = Intensity_Histograme(Frame_To_Analize(i, j), 1) + 1
                        Total_Number_of_good_hits(1) = Total_Number_of_good_hits(1) + 1


                        Frame_To_Analize(i - 1, j - 1) = 0
                        Intensity_Histograme(Frame_To_Analize(i - 1, j - 1), 1) = Intensity_Histograme(Frame_To_Analize(i - 1, j - 1), 1) + (Not (Neighbours(i - 1, j - 1) And Multiple_Hits(i - 1, j - 1, 2)))
                        Frame_To_Analize(i, j - 1) = 0
                        Intensity_Histograme(Frame_To_Analize(i, j - 1), 1) = Intensity_Histograme(Frame_To_Analize(i, j - 1), 1) + (Not (Neighbours(i, j - 1) And Multiple_Hits(i, j - 1, 2)))
                        Frame_To_Analize(i - 1, j) = 0
                        Intensity_Histograme(Frame_To_Analize(i - 1, j), 1) = Intensity_Histograme(Frame_To_Analize(i - 1, j), 1) + (Not (Neighbours(i - 1, j) And Multiple_Hits(i - 1, j, 2)))
                        Frame_To_Analize(i, j) = 0
                        Intensity_Histograme(Frame_To_Analize(i, j), 1) = Intensity_Histograme(Frame_To_Analize(i, j), 1) + (Not (Neighbours(i, j) And Multiple_Hits(i, j, 2)))
                    End If
                End If
            Next
        Next

    End Sub

End Module
